//
//  DDContactRequestCache.m
//  DingDongZP
//
//  Created by Ray on 16/4/27.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDContactRequestCache.h"

@implementation DDContactRequestCache


- (BOOL)didSendRequest:(DDContactRequestType)request
            withUserId:(NSString *)userid
{
    NSString *key = [NSString stringWithFormat:@"request_%@_%ld", userid, (long)request];
    if ([_cache objectForKey:key]) {
        return YES;
    }
    
    /// 如果没有缓存，那么在这里就直接写入缓存
    [_cache setObject:@(YES) forKey:key];
    return NO;
}

- (BOOL)didReplyRequest:(DDContactRequestType)request
             withUserId:(NSString *)userid
{
    NSString *key = [NSString stringWithFormat:@"reply_%@_%ld", userid, (long)request];
    if ([_cache objectForKey:key]) {
        return YES;
    }
    
    /// 如果没有缓存，那么在这里就直接写入缓存
    [_cache setObject:@(YES) forKey:key];
    return NO;
}

+ (instancetype)sharedCache {
    static DDContactRequestCache *__sharedCache = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        __sharedCache = [[DDContactRequestCache alloc] init];
    });
    return __sharedCache;
}

- (instancetype)init
{
    self = [super init];
    _cache = [[YYCache alloc] initWithName:@"DDRequestCache"];
    [_cache removeAllObjects];
    return self;
}

- (BOOL)didChatWithUser:(NSString *)userid {
    NSString *key = [NSString stringWithFormat:@"chat_%@", userid];
    if ([_cache objectForKey:key]) {
        return YES;
    }
    [_cache setObject:@(YES) forKey:key];
    return NO;
}


- (id)cachedJobWithUserId:(NSString *)userid {
    NSString *key = [NSString stringWithFormat:@"job_%@", userid];
    return [_cache objectForKey:key];
}

- (void)cacheJob:(id)job withUserId:(NSString *)userid {
	NSString *key = [NSString stringWithFormat:@"job_%@", userid];
    if (job) {
        [_cache setObject:job forKey:key];
    }
}

@end
